var GridControl = {
	totalCount: 0,
	actionBack: null,
	//绑定数据源
	Bind: function (d, divname, colSetting) {
		var self = this;
		$("#" + divname).html('');
		var data = null;
		//列排序开始
		var colarr = [];
		for (var obj in colSetting) {
			colSetting[obj]["key"] = obj;
			colSetting[obj]["visible"] = colSetting[obj]["visible"] == false ? colSetting[obj]["visible"] : true;
			colarr.push(colSetting[obj])
		}
		colarr.sort(function (a, b) {
			return (a.index ? a.index : 0) - (b.index ? b.index : 0);
		});
		//列排序结束
		if (d) {
			data = d.List == undefined ? this.EchoEntity(d) : this.EchoEntity(d.List);
			if (d.List) {
				this.totalCount = d.PageCount;
			}
		}
		var $table = $("<div></div>").addClass("table");
		var isTitle = true;
		if (!data||data.length<=0) {
			//如果数据为空，只打印表头
			if (isTitle) {
				var $title_row = $("<div></div>").addClass("table-tr").addClass("table-title");
				for (var i = 0; i < colarr.length; i++) {
					if (!colarr[i].visible) {
						continue;
					}
					var $th = $("<div></div>").addClass("table-th");
					$th.html(colarr[i].cnName);
					$title_row.append($th);
				}
				$table.append($title_row);
				isTitle = false; //不再打印表头
			}

			$("#" + divname).append($table);
			return false;
		}
		data.forEach(function (d, di) {
			if (isTitle) {
				var $title_row = $("<div></div>").addClass("table-tr").addClass("table-title");
				for (var i = 0; i < colarr.length; i++) {
					if (!colarr[i].visible) {
						continue;
					}
					d.columns.forEach(function (col, index) {
						var colObj = null;
						if (colarr[i].key == col.title) {
							colObj = colarr[i];
						}
						if (colObj != null) {
							var $th = $("<div></div>").addClass("table-th");
							$th.html(colObj.cnName);
							$title_row.append($th);
						}
					});
					$table.append($title_row);
					isTitle = false; //不再打印表头
				}
			}
			var $row = $("<div></div>").addClass("table-tr");
			if (!d) return false;
			var rowobj = {};
			for (var i = 0; i < colarr.length; i++) {
				d.columns.forEach(function (col, index) {
					rowobj[col.title] = col.val;
					if (colarr[i].visible) {
						var colObj = null;
						//添加转换器
						var convert = null;
						if (colarr[i].key == col.title) {
							colObj = colarr[i];
						}
						if (colObj != null) {
							rowobj[col.title] = col.val;
							convert = colObj["converter"];
							var $th = $("<div></div>").addClass("table-th");
							if (convert != null) {
								var _html = convert(col.val, rowobj);
								$th.html(_html);
							} else {
								$th.html(col.val == null || col.val == 'null' ? '' : col.val);
							}
							//$th.html(col.val);
							$row.append($th);
						}
					}
				});
			}
			//遍历所有列
			// d.columns.forEach(function(col,index){
			// 	var colObj = null;
			// 	//添加转换器
			// 	var convert = null;
			// 	for(var settingField in colSetting){
			// 	    if(settingField==col.title){
			// 			colObj = colSetting[settingField];
			// 	    }
			// 	}
			// 	if(colObj != null)
			// 	{
			// 		convert = colObj["converter"];
			// 		var $th = $("<div></div>").addClass("table-th");
			// 		if(convert!=null){
			// 			var _html = convert(col.val);
			// 			$th.html(_html);
			// 		}else{
			// 			$th.html(col.val==null||col.val=='null'?'':col.val);
			// 		}
			// 		//$th.html(col.val);
			// 		$row.append($th);
			// 	}
			// });
			//交互效果，css实现
			// $row.mouseenter(function(){
			// 	$(this).css("background","#EE6131");
			// 	$(this).css("color","#FEFEFE");
			// });
			// $row.mouseout(function(){
			// 	$(this).css("background","#fefefe");
			// 	$(this).css("color","#222");
			// });
			$row.off('click').on('click', function () {
				$(this).addClass("ppm-active").siblings('.table-tr').removeClass("ppm-active");
			});
			$table.append($row);
		});
		$("#" + divname).append($table);
		$(".ppm-btn-action").off('click').on('click', function () {
			typeof (self.actionBack) == 'function' && self.actionBack($(this));
		});
	},
	//将接口返回的Entity对象兑入表格对象中
	EchoEntity: function (data) {
		var iEnumerate = []; //列表对象
		data.forEach(function (d, i) {
			var obj = new Object();
			obj.columns = []; //所有属性和值
			for (var field in d) {
				var col = new Object();
				eval("col.title='" + field + "';");
				eval("col.val='" + d[field] + "';");
				obj.columns.push(col);
			}
			iEnumerate.push(obj);
		});
		return iEnumerate;

	},
}